package com.ocom.payconfig.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.payconfig.EntitySubsidyData;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.request.payconfig.AddSubsidyDataResquest;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoSubsidyData;
import com.ocom.payconfig.service.SubsidyDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SubsidyDataServiceImpl extends ServiceImpl<IDaoSubsidyData, EntitySubsidyData> implements SubsidyDataService {

    @Autowired
    private IDaoSubsidyData iDaoSubsidyData;


    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public List<EntitySubsidyData> subsidyList(Long comId, String strPlanID) {
        QueryWrapper<EntitySubsidyData> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("month_value","times",
                "SUM(plan_money) AS planAmount",
                "SUM(case when plan_money = 0 then 0 else 1 end) AS planNumber",
                "SUM(case when data_status = 4  then complete_money else 0 end) AS completeAmount",
                "SUM(case when data_status = 4  then 1 else 0 end) AS completeNumber",
                "SUM(clear_money) AS clearAmount",
                "SUM(case when clear_money = 0 then 0 else 1 end) AS clearNumber");
        queryWrapper.eq("com_id",comId);
        String[] arr=strPlanID.split(",");
        queryWrapper.in("plan_id",arr);
        queryWrapper.groupBy("month_value","times");
        return iDaoSubsidyData.selectList(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public List<EntitySubsidyData> cloneSubsidyData(Long comId, JSONObject jsonObject) {
        return iDaoSubsidyData.oldData(comId,jsonObject.getInteger("oldSubsidyId"),jsonObject.getIntValue("wallet"));
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public Boolean subsidyDataSaves(Long comId, List<EntitySubsidyData> saveEntitySubsidyData) {
        return this.saveBatch(saveEntitySubsidyData);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public List<EntitySubsidyData> StaySubsidyData(Long comId, Integer planId) {
        return iDaoSubsidyData.StaySubsidyData(planId);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public Boolean updateSubsidy(Long comId, Integer planId, Integer completeMoney, Long clearMoney, Date date) {
        UpdateWrapper<EntitySubsidyData> updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("complete_money",completeMoney);
        updateWrapper.set("clear_money",-clearMoney);
        updateWrapper.set("grant_time",date);
        updateWrapper.set("data_status",4);
        updateWrapper.eq("id",planId);
        return this.update(updateWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public Boolean updateSid(Long comId, Integer id, Integer dataStatus, Date date) {
        UpdateWrapper<EntitySubsidyData> updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("grant_time",date);
        if (dataStatus !=0){
            updateWrapper.set("data_status",dataStatus);
        }
        updateWrapper.eq("id",id);
        return this.update(updateWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public Boolean removeSubsidyData(Long comId, AddSubsidyDataResquest addSubsidyDataResquest, Integer planId) {
        QueryWrapper<EntitySubsidyData> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("plan_id",planId);
        String[] arr=addSubsidyDataResquest.getPIds().split(",");
        queryWrapper.in("p_id",arr);
        queryWrapper.eq("data_status",1);
        return this.remove(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public Boolean updateSubsidyMoney(Long comId, AddSubsidyDataResquest addSubsidyDataResquest, Integer planId) {
        UpdateWrapper<EntitySubsidyData> updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("update_time",new Date());
        updateWrapper.set("plan_money",addSubsidyDataResquest.getPlanMoney());
        updateWrapper.set("update_userid",addSubsidyDataResquest.getUserId());
        updateWrapper.eq("plan_id",planId);
        String[] arr=addSubsidyDataResquest.getPIds().split(",");
        updateWrapper.in("p_id",arr);
        updateWrapper.eq("data_status",1);
        updateWrapper.ne("plan_money",addSubsidyDataResquest.getPlanMoney());
        return this.update(updateWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public EntitySubsidyData selectOrderNo(Long comId, String orderNo) {
        QueryWrapper<EntitySubsidyData> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",comId);
        queryWrapper.eq("order_no",orderNo);
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public boolean insetSubsidyData(Long comId, EntitySubsidyData entitySubsidyData) {
        return this.save(entitySubsidyData);
    }

    @Override
    public boolean updatePlanMoney(Long comId, Integer planMoney, String orderNo) {
        UpdateWrapper<EntitySubsidyData> updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("plan_money",planMoney);
        updateWrapper.eq("com_id",comId);
        updateWrapper.eq("order_no",orderNo);
        return this.update(updateWrapper);
    }

}
